#include "StdAfx.h"
#include "CJawt.h"

JAWT *g_jawt;

CJawt::CJawt()
:m_surface(NULL)
,m_info(NULL)
,m_sysinfo(NULL)
,m_lockstatus(0)
,m_hdc(0)
,m_error(NULL)
{
}

CJawt::~CJawt(void)
{
	if(m_surface)
	{
		if(m_info)
			m_surface->FreeDrawingSurfaceInfo(m_info);
		m_surface->Unlock(m_surface);
		g_jawt->FreeDrawingSurface(m_surface);
	}
}

BOOL CJawt::init( JNIEnv *env,jobject glcanvas )
{
	//env->FindClass("java.lang.Exception");

	if(g_jawt || (g_jawt=new jawt,g_jawt->version=JAWT_VERSION_1_3,JAWT_GetAWT(env,g_jawt)))
	{
		m_surface=g_jawt->GetDrawingSurface(env,glcanvas);
		if(m_surface)
		{
			m_lockstatus=m_surface->Lock(m_surface);
			if(m_lockstatus & JAWT_LOCK_ERROR)
			{
				g_jawt->FreeDrawingSurface(m_surface);
				m_error=_T("Error locking surface.");
				return FALSE;
			}
			else
			{
				m_info=m_surface->GetDrawingSurfaceInfo(m_surface);
				if(m_info)
				{
					m_sysinfo=reinterpret_cast<jawt_Win32DrawingSurfaceInfo*>(m_info->platformInfo);
					m_hdc=m_sysinfo->hdc;
					return TRUE;
				}
				else
				{
					m_surface->Unlock(m_surface);
					g_jawt->FreeDrawingSurface(m_surface);
					m_error=_T("Error getting surface info.");
					return FALSE;
				}
			}
		}
		else
		{
			m_error=_T("NULL drawing surface.");
			return FALSE;
		}
	}
	else
	{
		m_error=_T("AWT not found.");
		return FALSE;
	}
}
